package com.hongdee.base.imp.service.impl;

import com.hongdee.base.imp.dao.IDataEntityDao;
import com.hongdee.base.imp.entity.TDataEntity;
import com.hongdee.base.imp.service.IDataEntityService;
import com.hongdee.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/14.
 */
@Service
public class DataEntityServiceImpl implements IDataEntityService {

    private static final String ENTITY_TABELE = "T_DATA_ENTITY";
    @Autowired
    private IDataEntityDao dataEntityDao;

    @Override
    public Map page(String start, String limit, String strName) throws Exception {
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("select * from " + ENTITY_TABELE + " where 1=1 ");
        List<String> paramList = new ArrayList<String>();
        if (!StringUtils.isEmpty(strName)) {
            sqlBuffer.append(" and ( entity_title like ?");
            paramList.add("%" + strName + "%");
            sqlBuffer.append(" or qualified_name like ? )");
            paramList.add("%" + strName + "%");
        }


        return this.dataEntityDao.page(sqlBuffer.toString(), paramList.toArray(), start, limit);

    }

    /**
     * 更新状态、停用、启用
     *
     * @param id
     * @param status
     * @throws Exception
     */
    @Override
    public void update(String id, String status) throws Exception {

        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录");
        }
        String hql = " update TDataEntity set status =:status where id = :id";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.dataEntityDao.update(hql, params);
    }

    @Override
    public void saveOrUpdate(TDataEntity de, String uid) throws Exception {

        List params = new ArrayList();
        String sql = "select qualified_Name from t_data_entity where 1=1 and qualified_Name = ?";
       // params.add(TokenUtil.getCompanyId(uid));
        params.add(de.getQualifiedName());

        if (de.getId() != null) {
            sql += " and id != ? ";
            params.add(de.getId());
        }

//        List<Map<String, Object>> list = this.dataEntityDao.list(sql, params.toArray());
//        if (null != list && list.size() > 0) {
//            throw new Exception("实体名已存在！");
//        }

        dataEntityDao.saveOrUpdate(de);

    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Map info(String id) throws Exception {

        return dataEntityDao.info(ENTITY_TABELE, "id", id);

    }

    /**
     * 查询实体下拉框
     *
     * @return
     * @throws Exception
     */
    @Override
    public List combo() throws Exception {
        String sql = " select * from " + ENTITY_TABELE + " where status ='1' ";
        List<Map<String, Object>> mapList = this.dataEntityDao.list(sql);
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("label", map.get("entityTitle"));
            temp.put("value", map.get("id"));
            resList.add(temp);
        }
        return resList;
    }


}
